﻿using BLL;
using DBUtility;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IYAKASAPortal.PrintAll
{
    public partial class ZDLQX : System.Web.UI.Page
    {

        private string m_AJLSH;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["AJLSH"] != null && Request["AJLSH"].ToString() != "")
            {
                m_AJLSH = Request["AJLSH"].ToString();
               TextArea1.InnerText = GetQXJSONStr(GetByteTbale(m_AJLSH));
            }
        }

        public string GetJsonByAJLSH()
        {
                return GetQXJSONStr(GetByteTbale(m_AJLSH));
        }

        private DataTable GetByteTbale(string AJLSH)
        {
            DataTable dt = new DataTable();
            string strSql = string.Format("SELECT  YZZDQX,EZZDQX,SZZDQX,SIZZDQX,WZZDQX,LZZDQX ,VMAS FROM RESULT_PROCESS_LINE WHERE JCLSH = '{0}'",AJLSH);
            DbHelper.GetTable(strSql, ref dt);
            return dt;
        }
        private string GetQXJSONStr(DataTable dt)
        {    
            List<ChartEntity> rtList = new List<ChartEntity>();
            if (dt.Rows.Count > 0)
            {
                for (int colCount = 0; colCount < dt.Columns.Count; colCount++)
                {
                    if (dt.Rows[0][colCount] != null && !dt.Rows[0].IsNull(colCount))
                    {
                        byte[] b = (byte[])dt.Rows[0][colCount];
                        ChartEntity entity = new ChartEntity();
                        _CURVE_INFO c = new _CURVE_INFO(1);
                        Int32 size = Marshal.SizeOf(c.GetType());
                        IntPtr buffer = Marshal.AllocHGlobal(2640);
                        if (b.Length > 2640)
                        {

                            try
                            {
                                Marshal.Copy(b, 0, buffer, 2640);
                                Object obj = Marshal.PtrToStructure(buffer, c.GetType());
                                string s = Encoding.GetEncoding("GBK").GetString(((_CURVE_INFO)obj).AxisName);
                                string[] str = s.Split('\0');

                                List<string> list = new List<string>();
                                foreach (var item in str)
                                {
                                    if (item != string.Empty)
                                    {
                                        list.Add(item);
                                    }
                                }
                                int x = ((_CURVE_INFO)obj).DataNum;
                                int t = size;
                                size = 2640;

                                List<string> xList = new List<string>();
                                Dictionary<int, List<double>> dic = new Dictionary<int, List<double>>();
                                Dictionary<int, List<LinePoint>> dicPoint = new Dictionary<int, List<LinePoint>>();

                                double maxValue = 0;
                                double maxValueX = 0;
                                for (int i = 0; i < x; i++)
                                {
                                    byte[] newByte = b.Skip(size).Take(sizeof(float)).ToArray();
                                    double xPoint = Convert.ToDouble(Math.Round(BitConverter.ToSingle(newByte, 0), 3));
                                    maxValueX = maxValueX > xPoint ? maxValueX : xPoint;
                                    xList.Add(Convert.ToDouble(Math.Round(BitConverter.ToSingle(newByte, 0), 3)).ToString());
                                    size += 4;
                                    for (int j = 0; j < ((_CURVE_INFO)obj).AxisNum; j++)
                                    {
                                        LinePoint pointEntity = new LinePoint();
                                        byte[] yByte = b.Skip(size).Take(sizeof(float)).ToArray();
                                        double yPoint = Math.Round(Convert.ToDouble(BitConverter.ToSingle(yByte, 0)), ((_CURVE_INFO)obj).yzDec);
                                        pointEntity.XPoint = Math.Round(xPoint, ((_CURVE_INFO)obj).xzDec);
                                        pointEntity.YPoint = Math.Round(yPoint, ((_CURVE_INFO)obj).yzDec);

                                        if (!dic.Keys.Contains(j))
                                        {
                                            dic.Add(j, new List<double>());
                                        }

                                        if (!dicPoint.Keys.Contains(j))
                                        {
                                            dicPoint.Add(j, new List<LinePoint>());
                                        }

                                        maxValue = maxValue > yPoint ? maxValue : yPoint;
                                        dic[j].Add(yPoint);
                                        dicPoint[j].Add(pointEntity);
                                        size += 4;
                                    }
                                }

                                List<LineEntity> listLineEntity = new List<LineEntity>();
                                for (int opi = 0; opi < list.Count; opi++)
                                {
                                    LineEntity entity1 = new LineEntity();
                                    entity1.LineName = list[opi];
                                    entity1.PointList = dicPoint[opi];
                                    listLineEntity.Add(entity1);

                                }

                                entity.LineList = listLineEntity;
                                entity.ChartName = ((_CURVE_INFO)obj).CurveTer;
                                entity.XName = ((_CURVE_INFO)obj).xzName;
                                entity.YName = ((_CURVE_INFO)obj).yzName;
                                entity.XDec = ((_CURVE_INFO)obj).xzDec;
                                entity.YDec = ((_CURVE_INFO)obj).yzDec;
                                entity.XList = xList;

                                List<List<double>> tmpList = new List<List<double>>();
                                foreach (var item in dic.Values)
                                {
                                    tmpList.Add(item);
                                }
                                entity.YList = tmpList;
                                entity.MaxYValue = maxValue;
                                entity.MaxXValue = maxValueX;
                                entity.CheckType = m_AJLSH.Substring(0, 1);
                                rtList.Add(entity);
                            }
                            finally
                            {
                                Marshal.FreeHGlobal(buffer);
                            }
                        }
                    }
                }
            }
            return JsonConvert.SerializeObject(rtList);
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct _CURVE_INFO
        {
            [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string CurveTer;
            [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string FiledName;
            public short AxisNum;
            [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string xzName;
            [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string yzName;
            public byte xzDec;
            public byte yzDec;
            [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 2500)]
            public byte[] AxisName;
            public short DataNum;
            public int DataLen;
            [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 50)]
            public byte[] yzDecEx;

            public _CURVE_INFO(int s)
            {
                yzName = "";
                CurveTer = "";
                FiledName = "";
                xzName = "";
                DataLen = 0;
                DataNum = 0;
                xzDec = 0;
                yzDec = 0;
                AxisNum = 0;
                AxisName = new byte[2500];
                yzDecEx = new byte[50];

            }
        };

        public class ChartEntity
        {

            public int IsLoad { get; set; }

            //图表名称
            public string ChartName { get; set; }

            //Y轴名称
            public string YName { get; set; }

            //X轴名称
            public string XName { get; set; }

            public List<LineEntity> LineList { get; set; }

            public List<string> XList { get; set; }

            public List<List<double>> YList { get; set; }

            public double MaxYValue { get; set; }

            public double MaxXValue { get; set; }

            public int XDec { get; set; }

            public int YDec { get; set; }

            public string CheckType { get; set; }            


        }

        public class LineEntity
        {
            public string LineName { get; set; }

            public List<LinePoint> PointList { get; set; }

            public double MaxY {
                get
                {
                    double val = 0;
                    foreach (var item in PointList)
                    {
                        val = val > item.YPoint ? val : item.YPoint;
                    }
                    return val;
                }
            }

            public double MaxX
            {
                get
                {
                    double val = 0;
                    foreach (var item in PointList)
                    {
                        val = val > item.XPoint ? val : item.XPoint;
                    }
                    return val;
                }
            }
            
            
        }

        public class LinePoint
        {
            public double XPoint { get; set; }

            public double YPoint { get; set; }
        }
    }
}